export default{
  // 开启命名空间
  namespaced: true,
  
  // state数据
  state: () => ({
    // 收货地址
    // 读取本地收货地址信息，初始化address数据
    address: JSON.parse(uni.getStorageSync('address')|| '{}'),
    // 登陆成功后的字符串
    token:uni.getStorageSync('token')||'',
    userInfo: JSON.parse(uni.getStorageSync('userInfo')|| '{}'),
    // 重定向的object对象{opentype,from}
    redirectInfo:null
  }),
  // 方法
  mutations:{
    // 更新收货地址
    updateAddress(state,address){
      state.address = address
      this.commit('m_user/saveAddressToStorage')
    },
    updateUserInfo(state,userInfo){
      state.userInfo = userInfo
      this.commit('m_user/saveUserInfoToStorage')
    },
    // 持久化存储
    saveAddressToStorage(state){
      uni.setStorageSync('address',JSON.stringify(state.address))
    },
    saveUserInfoToStorage(state){
      uni.setStorageSync('userInfo',JSON.stringify(state.userInfo))
    },
    updateToken(state,token){
      state.token = token
      this.commit('m_user/saveTokenToStorage')
    },
    saveTokenToStorage(state){
      uni.setStorageSync('token',state.token)
    },
    updateRedirectInfo(state,info){
      state.redirectInfo = info
    }
  },
  // 数据包装器
  getters:{     
    // 收获详细地址的计算属性
      addstr(state){
        if(!state.address.provinceName) return ''
        // 拼接省市区详细地址的字符串返回给用户
        return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
      }
    },
}